package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.BaseException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.utils.AliOssUtil;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class DishServiceImpl implements DishService {
	@Autowired
	DishMapper dishMapper;

	@Autowired
	FlavorMapper flavorMapper;

	@Autowired
	AliOssUtil aliossUtil;

	@Override
	public DishVO dishById(Long id) {
		DishVO dishvo = dishMapper.dishById(id);
		//使用外接表查询菜品类型
		log.info("查询菜品基础信息{}",dishvo.toString());
		//查询菜品口味
		List<DishFlavor> ld = flavorMapper.dishFlavors(id);
		log.info("菜品{}口味:{}",dishvo.getName(), ld.toString());
		dishvo.setFlavors(ld);
		//判断是否查询成功
		if(dishvo == null){
			throw new BaseException("查询失败");
		}
		log.info("查询结果{}",dishvo);
		return dishvo;
	}

	/**
	 * 分页查询菜品
	 * @param queryDTO
	 * @return
	 */
	@Override
	public PageResult page(DishPageQueryDTO queryDTO) {
		//查询全部菜品
		Integer total = dishMapper.total();
		log.info("共有{}个菜品",total);
		//计算起始页面
		queryDTO.setPage((queryDTO.getPage()-1) * queryDTO.getPageSize());
		List<DishVO> ld =  dishMapper.page(queryDTO);
		if(ld == null){
			throw new BaseException("分页查询失败");
		}
		log.info("查询结果:{}",ld);
		//填充返回字段
		PageResult pr = new PageResult();
		pr.setTotal(total);
		pr.setRecords(ld);
		return pr;
	}

	/**
	 * 设置菜品状态
	 * @param status
	 * @param id
	 */
	@Override
	@Transactional
	public void status(Integer status, Long id) {
		Dish dish = Dish.builder().id(id).status(status).build();
		Integer ret = dishMapper.status(dish);
		log.info("更改结果:{}", ret);
		if(ret == null){
			//更改状态失败
			throw new BaseException("修改失败,请重新尝试");
		}
	}

	/**
	 * 修改菜品信息
	 * @param dishDTO
	 */
	@Override
	@Transactional
	public void update(DishDTO dishDTO) {
		Long dishId = dishDTO.getId();
		log.info("设置菜品id:{}",dishId);
		log.info("{}",dishDTO.toString());
		//填充默认字段
		Dish dish = Dish.builder().id(dishDTO.getId()).status(dishDTO.getStatus())
					.categoryId(dishDTO.getCategoryId()).name(dishDTO.getName())
				    .price(dishDTO.getPrice()).image(dishDTO.getImage())
					.description(dishDTO.getDescription()).build();

		//更新菜品口味
		//1:先把旧数据删除
		flavorMapper.deleteById(dishDTO.getId());
		//2:重新插入新数据
		//获取新设置的菜品口味
		List<DishFlavor> dl = dishDTO.getFlavors();
		if(dl != null && !dl.isEmpty()){
			for(DishFlavor df : dl){
				log.info("{}",df);
				//与菜品关联
				df.setDishId(dishDTO.getId());
				//插入新口味
				Integer ret = flavorMapper.insert(df);
				if(ret == null){
					throw new BaseException();
				}
			}
		}
		//更新菜品
		log.info("更新菜品{}",dish);
		Integer ret = dishMapper.update(dish);
		if(ret == null){
			throw new BaseException();
		}
	}

	/**
	 * 添加菜品
	 * @param dishDTO
	 */
	@Override
	@Transactional
	public void add(DishDTO dishDTO) {
		log.info("dishDTO:{}",dishDTO);
		Dish dish = new Dish();
		//填充默认字段
		BeanUtils.copyProperties(dishDTO, dish);
		//默认停售菜品
		dish.setStatus(StatusConstant.DISABLE);
		//插入菜品
		Integer ret = dishMapper.insert(dish);
		if(ret == null){
			throw new BaseException("插入失败");
		}
		//打开id回显获取菜品id
		Long dishId = dish.getId();
		//获取菜品口味
		List<DishFlavor> Lf = dishDTO.getFlavors();
		//批量插入菜品口味
		for(DishFlavor df : Lf){
			log.info("插入菜品{}的口味:{}",dishId, df);
			//建立口味与菜品关系
			df.setDishId(dishId);
			//插入菜品口味
			ret = flavorMapper.insert(df);
			if(ret == null){
				throw new BaseException("插入失败");
			}
		}
	}

	/**
	 * 批量删除从菜品
	 * @param idList
	 */
	@Override
	@Transactional
	public void deleteBatch(List<Long> idList) {

		for(Long id: idList){
			//删除菜品相关资源图片
			String image = dishMapper.image(id);
			log.info("删除图片资源:{}",image);
			String fileName = image.substring(image.lastIndexOf("/") + 1);
			aliossUtil.delete(fileName);
			//删除与菜品关联的口味
			log.info("删除与菜品相关口味");
			flavorMapper.deleteById(id);
		}
		log.info("批量删除菜品");
		dishMapper.deleteBatch(idList);
	}
}
